package com.liulichev.omstest;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileUtils {

	public static String extractZippedMapFile(InputStream zipFileInputStream,
			String destFolder) throws IOException {
		ZipInputStream zis = new ZipInputStream((new BufferedInputStream(
				zipFileInputStream)));
		ZipEntry entry = zis.getNextEntry();
		String unzippedFileDestination = destFolder + entry.getName();
		FileOutputStream fos = new FileOutputStream(unzippedFileDestination);

		byte[] buffer = new byte[2048];
		int size;
		BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

		while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
			bos.write(buffer, 0, size);
		}
		bos.flush();
		bos.close();
		File targetFile = new File(unzippedFileDestination);
		if (targetFile.exists())
			return targetFile.getAbsolutePath();

		throw new RuntimeException("Cannot unzip file");
	}

}